<?php namespace Home\Controller;

use \Common\Model\Status;
use \Common\Model\User;
use \Common\Model\Company;
use \Common\Model\City;

//投递箱控制器
class StatusController extends CommonController{
	protected $statusModel;
	public function __auto(){
		$this->statusModel=new Status();
		$userModel=new User();
		//审核权限
		$uid=$_SESSION['homeInfo']['uid'];
		$type=$userModel->where("uid={$uid}")->pluck('type');
		if($type==1) View::error('抱歉，您没有该权限',__ROOT__.'/index.html');
	}

	//全部简历状态
	public function index(){
		$this->getData();
		View::make();
	}

	//投递成功的简历
	public function deliver(){
		$this->getData(0);
		View::make();
	}

	//待沟通的简历
	public function reply(){
		$this->getData(1);
		View::make();
	}

	//邀请面试的简历
	public function facetime(){
		$this->getData(2);
		View::make();
	}

	//不合适的简历
	public function mismatch(){
		$this->getData(3);
		View::make();
	}

	//投递简历
	public function add(){
		if(!isset($_SESSION['homeInfo']['uid'])) View::error('请先登录',__ROOT__.'/login.html');
		$uid=$_SESSION['homeInfo']['uid'];
		$userModel=new User();
		//判断用户是否已上传简历
		$resume=$userModel->where("uid=$uid")->pluck('resume');
		if(!$resume) View::error('请先上传简历',__ROOT__.'/resume.html');
		//投递简历
		if (!$this->statusModel->store()) View::error('投递简历出现异常');
		View::success('投递成功');
	}

	//获得简历状态数据
	private function getData($sta='all'){
		switch ($sta) {
			case '0':
				$str='0';
				break;
			case '1':
				$str='1';
				break;
			case '2':
				$str='2';
				break;
			case '3':
				$str='3';
				break;
			default:
				$str='0,1,2,3';
				break;
		}
		$uid=$_SESSION['homeInfo']['uid'];
		$data=$this->statusModel
			->join('job','lg_job_jid','=','jid')
			->where("lg_user_uid=$uid AND status IN({$str})")
			->orderBy('delivertime','DESC')
			->get();
		$companyModel=new Company();
		$cityModel=new City();
		$userModel=new User();
		foreach ($data as $key => $value) {
			//压入公司名称
			$gsData=$companyModel->where("gsid=".$data[$key]['lg_company_gsid'])->find();
			$data[$key]['gsname']=$gsData['gsname'];
			//压入HR信息
			$userData=$userModel->where("uid={$gsData['lg_user_uid']}")->find();
			$data[$key]['gsUsername']=$userData['username'];
			$data[$key]['gsEmail']=$userData['account'];

			//获得所属城市
			$pid=$cityModel->where("city_id={$data[$key]['lg_city_city_id']}")->pluck('city_pid');
			$city=$cityModel->where("city_id={$pid}")->pluck('city_name');
			$data[$key]['city']=$city;
			//判断投递状态
			switch ($data[$key]['status']) {
				case 0:
					$data[$key]['statusName']='投递成功';
					break;
				case 1:
					$data[$key]['statusName']='待沟通';
					break;
				case 2:
					$data[$key]['statusName']='已通知面试';
					break;
				default:
					$data[$key]['statusName']='不合适';
					break;
			}
		}
		//猜你喜欢
		$likeData=$companyModel
				->join('job','lg_company_gsid','=','gsid')
				->where("online=1")
				->orderBy('rand()')
				->limit(3)
				->get();
		foreach ($likeData as $k => $v) {
			//获得所属城市
			$pid=$cityModel->where("city_id={$likeData[$k]['lg_city_city_id']}")->pluck('city_pid');
			$city=$cityModel->where("city_id={$pid}")->pluck('city_name');
			$likeData[$k]['city']=$city;
		}
		View::with('likeData',$likeData);
		View::with('data',$data);
	}


}